package com.example.apple.testdown;

import android.app.Application;
import android.content.Context;
import android.os.Environment;
import android.widget.Toast;

import net.tsz.afinal.FinalDb;

import java.io.File;

/**
 * 作    者：高学军
 * 时    间：16/7/26
 * 描    述：
 * 修改时间：
 */
public class MyApplication extends Application{

    public static String VID = "vid";
    public static String DOWNLOADED = "downloaded";
    public static String FULLSIZE = "fullsize";

    //广播协议
    public static final String COURSE_RECEIVER_ACTION = "course_receiver_action";


    public static FinalDb db;
    public static Context context;
    //下载路径
    public static File fBaseDir;




    @Override
    public void onCreate() {
        super.onCreate();

        //初始化数据库
        context = getApplicationContext();
        db = FinalDb.create(context, false);

        getBaseDir();
    }

    public static void getBaseDir() {
        // 下载到SD卡
        if (Environment.getExternalStorageState().equals(
                android.os.Environment.MEDIA_MOUNTED)) {
            fBaseDir = context
                    .getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
        } else {
            fBaseDir = new File(MyApplication.context.getFilesDir(),
                    Environment.DIRECTORY_DOWNLOADS);
        }
        if (fBaseDir != null) {
            if (!fBaseDir.exists() && !fBaseDir.mkdir()) {
                Toast.makeText(context, "缓存目录不能写", Toast.LENGTH_SHORT).show();
            }

            if (!fBaseDir.isDirectory()) {
                Toast.makeText(context, "缓存目录名已存在但不是目录", Toast.LENGTH_SHORT)
                        .show();
            }
        }

    }

    public static File getImgDir() {
        File dir = new File(fBaseDir, "img");
        if (!dir.exists()) {
            dir.mkdir();
        }
        return dir;
    }


    public static File getVideoDir() {
        File dir = new File(fBaseDir, "video");
        if (!dir.exists()) {
            dir.mkdir();
        }
        return dir;
    }
}
